<?php

namespace App\Http\Controllers\Web\Seller;

use App\Repository\Models\Company;
use App\Repository\Models\Shop;
use App\Repository\Models\User;
use App\Repository\Repositories\Interfaces\CompanyMaterialRepository;
use App\Repository\Repositories\Interfaces\UserRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Web\BaseController;
use App\ConstDir\ErrorConst;


/**
 * 完善公司资料
 *
 * @package App\Http\Controllers\Backend
 */
class CompanyMaterialController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected $company_material_repo;

    public function __construct(CompanyMaterialRepository $company_material_repo , Request $request)
    {
        $this->company_material = $company_material_repo;
    }


    public function index()
    {


    }


    public function show($id)
    {

    }


    public function showUserCompany($user_id , UserRepository $user_repo)
    {
        $user_info = $user_repo->with(['company','company_material'])->find($user_id,['id','name','phone','identification_type','personal_enterprise']);

        return $this->success(ErrorConst::SUCCESS_CODE, $user_info);

    }


    public function saveCompanyMaterial(UserRepository $user_repo,Request $request) {
        $this->validate($request, [
            'ent_type' => 'required|json',
            'ent_property' => 'required|in:1,2,3,4,5,6,7,8,9,10',
            'ent_emp_number' => 'required|string',
            'ent_logo' => 'required|sometimes|string',
            'ent_service_tag' => 'required|sometimes|json',
            'ent_idea' => 'required|sometimes|string',
            'ent_intro' => 'required|sometimes|string',
            'contact_person' => 'required|string',
            'contact_sex' => 'required|integer|in:1,0',
            'contact_position' => 'required|sometimes|string',
            'contact_mobile' => 'required|string',
            'contact_tel' => 'required|sometimes|string',
            'contact_fax' => 'required|sometimes|string',
            'contact_address' => 'required|string',
            'contact_postcode' => 'required|sometimes|string|size:6',
            'business_model' => 'required|string',
            'business_address' => 'required|string',
            'business_trade' => 'required|json',
            'business_product' => 'required|json',
            'customer_groups' => 'required|sometimes|json',
            'sales_region' => 'required|json',
            'business_sum' => 'required|sometimes|string',
            'purchase_products' => 'required|sometimes|json',
            'purchase_region' => 'required|sometimes|json',
            'url' => 'required|sometimes|string',
        ]);
        $data = $request->all();
        $data['user_id'] = $data['userid'];

        $user = $user_repo->with(['company' => function ($query) {
            $query->select(['user_id', 'id', 'company_name', 'angent', 'step']);
        }, 'company_material'])->find($data['user_id']);
        $user_data = $user['data'];

        if (!empty($data['company_material_id'])) {
            if (empty($user_data['shop_id'])) {
                if ($user_data['identification_type'] == 2 && $user_data['company_material']) {
                    $add_data = [
                        'sp_name' => $user_data['company']['company_name'],
                        'sp_logo' => $data['ent_logo'],
                        'sp_uid' => $data['user_id'],
                        'sp_cmp_id' => $user_data['company']['id'],
                        'sp_level' => 2,
                    ];
                    $shop_id = Shop::query()->create($add_data)->toArray();
                    User::query()->where('id', $data['user_id'])->update(['shop_id' => $shop_id['sp_id']]);
                    $save_data['is_shop_open'] = 'yes';
                    Company::query()->where('id', $user_data['company']['id'])->update($save_data);
                }
            }else{
                Shop::query()->where('sp_id', $user_data['shop_id'])->update(['sp_logo'=>$data['ent_logo']]);

                Company::query()->where('id', $user_data['company']['id'])->update(['angent'=>$data['contact_person']]);
                $shop_id['sp_id'] = [];
            }
            $ret = $this->company_material->update($data, $data['company_material_id']);

        } else {
            //添加公司信息 开通店铺
            if ($ret = $this->company_material->create($data)) {

                if ($user_data['identification_type'] == 2) {
                    $add_data = [
                        'sp_name' => $user_data['company']['company_name'],
                        'sp_logo' => $data['ent_logo'],
                        'sp_uid' => $data['user_id'],
                        'sp_cmp_id' => $user_data['company']['id'],
                        'sp_level' => 2,
                    ];
                    $shop_id = Shop::query()->create($add_data)->toArray();
                    User::query()->where('id', $data['user_id'])->update(['shop_id' => $shop_id['sp_id'],'personal_enterprise'=>1]);
                    $save_data['is_shop_open'] = 'yes';
                    Company::query()->where('id', $user_data['company']['id'])->update($save_data);
                } else {
                    $shop_id['sp_id'] = [];
                }
            }
        }

        if ($ret && !empty($data['company_material_id'])) {
            return $this->success(ErrorConst::SUCCESS_CODE_MSG,$shop_id['sp_id']);
        } elseif ($ret && empty($data['company_material_id'])) {
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $shop_id['sp_id']);
        } else {
            return $this->error(ErrorConst::FAILED_CODE_MSG,$ret);

        }


    }


}
